cmake_minimum_required(VERSION 3.22)

#
#
# User is free to modify the file as much as necessary
#

# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)


# Define the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

# Set the project name
set(CMAKE_PROJECT_NAME UsbBtld)

# Include toolchain file
include("cmake/gcc-arm-none-eabi.cmake")

# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})

# Enable CMake support for ASM and C languages
enable_language(C ASM CXX)
enable_language(CXX)

file(GLOB_RECURSE SRC ${CMAKE_SOURCE_DIR}/App/*.c)
file(GLOB_RECURSE BSP ${CMAKE_SOURCE_DIR}/Bsp/*.c)
file(GLOB_RECURSE STM32_USB_DEVICE_CLASS_CDC ${CMAKE_SOURCE_DIR}/STM32_USB_Device_Library/*.c)
file(GLOB_RECURSE USB_DEVICE_APP ${CMAKE_SOURCE_DIR}/USB_Device/*.c)

include_directories(Bsp)

# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})


add_custom_command(
    TARGET  ${CMAKE_PROJECT_NAME}
    POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.bin
)

# Add STM32CubeMX generated sources
add_subdirectory(cmake/stm32cubemx)

# Add sources to executable
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user sources here
    ${STM32_USB_DEVICE_CLASS_CDC}
    ${USB_DEVICE_APP}
    ${BSP}
    ${SRC}
)

# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined include paths
    ${CMAKE_SOURCE_DIR}/Bsp
    ${CMAKE_SOURCE_DIR}/Core/Inc
    ${CMAKE_SOURCE_DIR}/STM32_USB_Device_Library/Class/CDC/Inc
    ${CMAKE_SOURCE_DIR}/STM32_USB_Device_Library/Core/Inc
    ${CMAKE_SOURCE_DIR}/USB_Device/App
    ${CMAKE_SOURCE_DIR}/USB_Device/Target
)

# Add project symbols (macros)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined symbols
)

# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
    stm32cubemx

    # Add user defined libraries
)
